package com.stu;

import io.mikael.urlbuilder.UrlBuilder;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: MI
 * @Date: 2021/10/25/21:42
 * @Description:
 */
public class UrlBuildes {
    /**
     * 访问的关键字
     */
    public static final String COOKIE_KEY_NAME = "kbzw__user_login";
    public String loginStr = "";

    public UrlBuildes() throws URISyntaxException {
    }

    public static void main1(String[] args) {
        UrlBuilder urlBuilder = UrlBuilder.fromString("https://www.jisilu.cn/question/429345");
        //sort_key=add_time&sort=DESC


        if (true) {
            urlBuilder = urlBuilder.addParameter("uid", "publisher");
        }
        urlBuilder = urlBuilder.addParameter("sort_key", "add_time")
                .addParameter("sort", "DESC");
        System.out.println(urlBuilder.toString());
    }

    @Test
    public void test() {
        String questionPage = "https://www.jisilu.cn/question/id-%s__sort_key-__sort-DESC__uid-%s__page-%s";
        System.out.println(String.format(questionPage, "sd", 2, 1));

        // String url = "https://www.jisilu.cn/question/id-429345__sort_key-__sort-DESC__uid-publisher__page-2";
        String url = "https://www.jisilu.cn/question/id-429345__sort_key-add_time__sort-DESC__uid-__page-2";
        login();
        try {
            String str = Jsoup.connect(url)
                    .header("Cookie", loginStr).method(Connection.Method.GET).execute().body();
            System.out.println(str);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    @Test
    public void login() {
        String urlLogin = "https://www.jisilu.cn/account/ajax/login_process/";
        Map<String, String> loginDataMap = new HashMap<>(4);
        loginDataMap.put("return_url", "https://www.jisilu.cn/");
        //my
        loginDataMap.put("user_name", "533c791817b16ee002a06a525f98e5dc");
        loginDataMap.put("password", "6a367d98cc9cf7fba310074afcbfdca1");
        loginDataMap.put("net_auto_login", "1");
        loginDataMap.put("_post_type", "ajax");
        loginDataMap.put("aes", "1");
        final Connection connect = Jsoup.connect(urlLogin);
        try {
            Connection.Response response = connect.method(Connection.Method.POST).ignoreContentType(true)
                    .ignoreHttpErrors(true)
                    .data(loginDataMap)
                    .followRedirects(false)
                    .execute();
            final Map<String, String> cookies = response.cookies();
            loginStr = COOKIE_KEY_NAME + "=" + cookies.get(COOKIE_KEY_NAME);
            System.out.println("登陆访问~成功：" + loginStr);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("登陆访问失败PostConstruct");
        }
    }

    @Test
    public void testUrlBuilder() throws URISyntaxException {

        //org.apache.http.client.utils.URIBuilder
        org.apache.http.client.utils.URIBuilder builder = new org.apache.http.client.utils.URIBuilder("http://app.jg.eastmoney.com/");
        builder.addParameter("name", "减持公司可转换");
        builder.addParameter("age", "1");
        System.out.println(builder.toString());

        UrlBuilder urlBuilder = UrlBuilder.fromString("https://www.jisilu.cn/");
        urlBuilder = urlBuilder.addParameter("name", "减持公司可转换");
        System.out.println(urlBuilder);

    }

}
